// 根据chapter获取对应的map+课程
const db = uniCloud.database();
const dbCmd = db.command;
const $ = db.command.aggregate;
module.exports = async function(options = {}) {
	const { chapter } = options;
	const token = this.getUniIdToken();
	if (!token || !chapter) {
		throw new Error('非法访问');
	}
	let [categoryList, categoryListCustom] = await Promise.all([
		db.collection('a-dz-category-list')
		.aggregate()
		.match({
			chapter
		})
		.lookup({
			from: 'a-dz-course-list',
			let: {
				mainid: '$category'
			},
			pipeline: $.pipeline()
				.match(dbCmd.expr($.eq(['$category', '$$mainid'])))
				.done(),
			as: 'courseList'
		})
		.end(),
		// 获取自定义的课程
		db.collection('a-dz-category-list-custom')
		.aggregate()
		.match({
			chapter
		})
		.lookup({
			from: 'a-dz-course-list-custom',
			let: {
				mainid: '$category'
			},
			pipeline: $.pipeline()
				.match(dbCmd.expr($.eq(['$category', '$$mainid'])))
				.done(),
			as: 'courseList'
		})
		.end()
	])
	return {
		code: 0,
		data: categoryList.data.concat(categoryListCustom.data),
		msg: '获取成功'
	}
}